package bean;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import dao.KlantDao;
import dao.KlantDaoImpl;
import entities.Klant;

/**
 * Message-Driven Bean implementation class for: QueueListener
 * 
 */
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }, mappedName = "jms/SaxtiehuisQueue")
public class QueueListenerBean implements MessageListener {
	private KlantDao kDao;

	/**
	 * Default constructor.
	 */
	public QueueListenerBean() {
		// TODO Auto-generated constructor stub
		kDao = new KlantDaoImpl();
	}

	/**
	 * @see MessageListener#onMessage(Message)
	 */
	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		System.out.println("Message received!");
		if (message instanceof ObjectMessage) {
			ObjectMessage objMessage = (ObjectMessage) message;
			try {
				Klant klant = (Klant) objMessage.getObject();
				kDao.updateKlantMetBestellingen(klant);
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
}
